package com.jooq.example.service.impl;

import com.jooq.example.config.db.DBProperties;
import com.jooq.example.config.db.DataSourceConfig;
import com.jooq.example.config.db.aop.TargetDataSource;
import com.jooq.example.domain.tables.daos.SysDeptDao;
import com.jooq.example.domain.tables.pojos.SysDept;
import com.jooq.example.domain.tables.records.SysDeptRecord;
import com.jooq.example.dto.PageModel;
import com.jooq.example.service.SysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.jooq.*;
import org.jooq.tools.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.jooq.example.domain.tables.SysDept.SYS_DEPT;

@Service
@Slf4j
public class SysDeptServiceImpl implements SysDeptService {

    @Autowired
    private SysDeptDao sysDeptDao;

    @Autowired
    private DSLContext dsl;

    @Override
    public void save(SysDept sysDept) {
        sysDeptDao.insert(sysDept);
    }

    @Override
    public int saveWithSelect(SysDept sysDept) {
//        int execute = dsl.insertInto(SYS_DEPT)
//                .columns(SYS_DEPT.ID, SYS_DEPT.NUM, SYS_DEPT.PID, SYS_DEPT.PIDS, SYS_DEPT.SIMPLENAME, SYS_DEPT.FULLNAME, SYS_DEPT.TIPS, SYS_DEPT.VERSION)
//                .values(sysDept.getId(), sysDept.getNum(), sysDept.getPid(), sysDept.getPids(), sysDept.getSimplename(), sysDept.getFullname(), sysDept.getTips(), sysDept.getVersion())
//                .execute();

        InsertSetStep<SysDeptRecord> insertSetStep = dsl.insertInto(SYS_DEPT);
        InsertSetMoreStep<SysDeptRecord> setMoreStep = null;
//        if(null != sysDept.getId()){
//            setMoreStep = insertSetStep.set(SYS_DEPT.ID, sysDept.getId());
//        }
        if (null != sysDept.getNum()) {
            setMoreStep = insertSetStep.set(SYS_DEPT.NUM, sysDept.getNum());
        }
        if (null != sysDept.getPid()) {
            setMoreStep = insertSetStep.set(SYS_DEPT.PID, sysDept.getPid());
        }
        if (!StringUtils.isBlank(sysDept.getPids())) {
            setMoreStep = insertSetStep.set(SYS_DEPT.PIDS, sysDept.getPids());
        }
        if (!StringUtils.isBlank(sysDept.getSimplename())) {
            setMoreStep = insertSetStep.set(SYS_DEPT.SIMPLENAME, sysDept.getSimplename());
        }
        if (!StringUtils.isBlank(sysDept.getFullname())) {
            setMoreStep = insertSetStep.set(SYS_DEPT.FULLNAME, sysDept.getFullname());
        }
        if (null != sysDept.getVersion()) {
            setMoreStep = insertSetStep.set(SYS_DEPT.VERSION, sysDept.getVersion());
        }
        InsertResultStep<SysDeptRecord> returning = setMoreStep.returning(SYS_DEPT.ID);
        log.info(returning.getSQL());
        log.info(returning.getParams().toString());
        SysDeptRecord sysDeptRecord = returning.fetchOne();
        log.info(sysDeptRecord.toString());
        sysDeptRecord.getId();
        return 0;
    }

    @Override
    public int update(SysDept sysDept) {
        sysDeptDao.update(sysDept);
        return 1;
    }

    @Override
    public int updateWithSelect(SysDept sysDept) {
        UpdateSetFirstStep<SysDeptRecord> update = dsl.update(SYS_DEPT);
        UpdateSetMoreStep<SysDeptRecord> set = null;
        if (null != sysDept) {
            set = update.set(SYS_DEPT.FULLNAME, sysDept.getFullname());
        }
        UpdateConditionStep<SysDeptRecord> where = set.where(SYS_DEPT.ID.eq(sysDept.getId())).and(SYS_DEPT.PID.eq(sysDept.getPid()));
        log.info(where.getSQL());
        log.info(where.getParams().toString());
        int execute = where.execute();
        return execute;
    }

    @Override
    public int delete(Integer id) {
        return dsl.delete(SYS_DEPT).where(SYS_DEPT.ID.eq(id)).execute();
    }

    @Override
    public SysDept queryById(Integer id) {
        return sysDeptDao.fetchOneById(id);
    }

    @Override
    public List<SysDept> list(SysDept sysDept) {
        return dsl.select().from(SYS_DEPT).fetchInto(SysDept.class);
    }

    /**
     * 对比 list 方法
     * 选择master数据源
     *
     * @param sysDept
     * @return
     */
    @TargetDataSource(DataSourceConfig.MASTER)
    @Override
    public List<SysDept> listm(SysDept sysDept) {
        return dsl.select().from(SYS_DEPT).fetchInto(SysDept.class);
    }

    @Override
    public List<SysDept> pageList(SysDept sysDept, PageModel pageModel) {
        SelectForUpdateStep<Record> offset = dsl.select().from(SYS_DEPT).where(SYS_DEPT.FULLNAME.like(fullLike(sysDept.getFullname()))).limit(pageModel.getOffset(), pageModel.getPageSize());
        log.info(offset.getSQL());
        log.info(offset.getParams().toString());
        return offset.fetchInto(SysDept.class);
    }

    private String fullLike(String value) {
        return new StringBuilder(3).append("%").append(value).append("%").toString();
    }

}
